import RouterInternal from "./internal"
import { Stack } from "../util";
export default class Router extends RouterInternal {
    private currentStack: Stack<RevealableRoute> = new Stack();
    private _root;
    constructor(root: HTMLIFrameElement, router: RouteConfig[], private notFoundPage: RouteConfig) {
        super(root, router);
        this.push(location.hash.slice(1) || "/");
        this._root = root;
        this.notFound(() => {
            this.replace(this.notFoundPage.path);
            console.log(this.notFoundPage.path);
        })
    }
    addRoute(item: RouteConfig) {
        this.addRouteItem(item);
    }
    back() {
        try {
            let res = this.currentStack.popItem();
            this.route(res.path);
        } catch (error) {
            console.log("没有页面可回退");
        }
    }
    push(path: string) {
        this.route(path);
        this.currentStack.pushItem(this.currentRoute);
    }
    replace(path: string) {
        this.route(path);
    }
    beforeEachLeave(cb: Function) {
        this.addBeforeLeave(cb);
    }
    afterEachEnter(cb: Function) {
        this.addAfterEnter(cb);
    }
    //为全局注入$router属性，表示当前的router实例
    install() {
        const that = this;
        window.$router = this;
        window.$route = this.currentRoute;
        this.addAfterEnter(function () {
            this.$router = that;
            this.$route = that.currentRoute;
        })
    }//根据routerConfig生成数组
}